﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RMS.DataAccess.Entities;
using RMS.DataAccess.Repositories;
using System.Web.Security;
using System.Web;
using RMS.Utils.Authentication;

namespace RMS.Web.Shell.Services
{
    public class AuthenticationService : IAuthentication
    {
        #region [ Fields ]
        private readonly EmployeesRepository _employeesRepository;
        #endregion

        #region [ Constructor ]
        public AuthenticationService(EmployeesRepository employeesRepository)
        {
            _employeesRepository = employeesRepository;
        }
        #endregion
        #region IAuthentication Members

        public ICurrentMember CurrentLogedInMember
        {
            get
            {
                return (CurrentUser)CurrentLogedInUser;
            }
        }

        #endregion

        #region [helper]
        public Employees CurrentLogedInUser
        {
            get
            {
                if (HttpContext.Current != null)
                {
                    var employees = HttpContext.Current.Items["CurrentLogedInUser"] as Employees;
                    if (employees == null)
                    {
                        employees =
                            _employeesRepository.Find(m => m.Username = HttpContext.Current.User.Identity.Name).Single();
                        HttpContext.Current.Items["CurrentLogedInUser"] = employees;
                    }
                    return employees;
                }
                return null;
            }
        }
        #endregion
    }
}
